package org.liushengpei.com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.liushengpei.com.constant.ResultMsgConstant;
import org.liushengpei.com.domain.GoodsAddress;
import org.liushengpei.com.mapper.GoodsAddressMapper;
import org.liushengpei.com.service.IGoodsAddressService;
import org.liushengpei.com.util.OnlyId;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 收货地址逻辑实现
 */
@Service
public class GoodsAddressServiceImpl extends ServiceImpl<GoodsAddressMapper, GoodsAddress> implements IGoodsAddressService {

    /**
     * 新增收货地址
     */
    @Override
    public String addGoodsAddress(GoodsAddress goodsAddress) {
        //判断是否是默认地址
        if (goodsAddress.getDefaultAddressOff() != 1) {
            return ResultMsgConstant.FAIL;
        }
        //修改其它收货地址为不是默认地址
        LambdaUpdateWrapper<GoodsAddress> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(GoodsAddress::getUserId, goodsAddress.getUserId())
                .eq(GoodsAddress::getDelFlag, 0)
                .set(GoodsAddress::getDefaultAddressOff, 0);
        this.update(wrapper);
        //添加收货地址
        goodsAddress.setId(OnlyId.nextId().toString());
        goodsAddress.setCreateTime(new Date());
        boolean isSave = this.save(goodsAddress);
        if (isSave) {
            return ResultMsgConstant.OK;
        }
        return ResultMsgConstant.FAIL;
    }

    /**
     * 修改收货地址
     */
    @Override
    public String editGoodsAddress(GoodsAddress goodsAddress) {
        //判断是否为默认地址
        if (goodsAddress.getDefaultAddressOff() != 1) {
            return ResultMsgConstant.FAIL;
        }
        //查询该地址是否为默认地址
        GoodsAddress isDefaultAddress = this.getById(goodsAddress.getId());
        if (isDefaultAddress.getDefaultAddressOff() != 1) {
            //修改其它收货地址为不是默认地址
            LambdaUpdateWrapper<GoodsAddress> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(GoodsAddress::getUserId, goodsAddress.getUserId())
                    .eq(GoodsAddress::getDelFlag, 0)
                    .set(GoodsAddress::getDefaultAddressOff, 0);
            this.update(wrapper);
        }
        //更新收货地址
        goodsAddress.setUpdateTime(new Date());
        boolean isUpdate = this.updateById(goodsAddress);
        if (isUpdate) {
            return ResultMsgConstant.OK;
        }
        return ResultMsgConstant.FAIL;
    }

    /**
     * 查询所有收货地址
     */
    @Override
    public List<GoodsAddress> queryListByUserId(String userId) {
        //查询条件构造器
        LambdaQueryWrapper<GoodsAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsAddress::getUserId, userId)
                .eq(GoodsAddress::getDelFlag, 0)
                .orderByDesc(GoodsAddress::getCreateTime);
        //查询该用户收货地址
        return this.list(queryWrapper);
    }

    /**
     * 删除收货地址
     */
    @Override
    public String delOneById(String id) {
        LambdaUpdateWrapper<GoodsAddress> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(GoodsAddress::getId, id)
                .set(GoodsAddress::getDelFlag, 1)
                .set(GoodsAddress::getUpdateTime, new Date());
        boolean isDel = this.update(updateWrapper);
        if (isDel) {
            return ResultMsgConstant.OK;
        }
        return ResultMsgConstant.FAIL;
    }

    /**
     * 查询收货地址详细信息
     */
    @Override
    public GoodsAddress queryDetail(String id) {
        return this.getById(id);
    }

    /**
     * 修改默认地址
     */
    @Override
    public String updateAddressTag(String id, String userId) {
        //取消其它默认地址
        LambdaUpdateWrapper<GoodsAddress> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(GoodsAddress::getUserId, userId)
                .eq(GoodsAddress::getDelFlag, 0)
                .set(GoodsAddress::getDefaultAddressOff, 0);
        this.update(updateWrapper);
        //修改该地址为默认地址
        GoodsAddress address = new GoodsAddress();
        address.setId(id);
        address.setDefaultAddressOff(1);
        address.setUpdateTime(new Date());
        boolean isEdit = this.updateById(address);
        if (isEdit) {
            return ResultMsgConstant.OK;
        }
        return ResultMsgConstant.FAIL;
    }
}
